// 总价格
function get_prices() {
    var ret = 0

    prices = $(".cart-subtotal")

    for (var i = 0; i < prices.length; i++) {

        var that = prices[i]

        console.log(parseInt($(that).html()))

        ret +=parseInt($(that).html())

    }

    $(".cart-total-price").html(ret)
}




// 小计价格

function subtoal() {

    var targets = $(".cart-subtotal")


    for (var i = 0; i < targets.length; i++) {
        // 获取质量
        var that = targets[i]

        var kg = $(that).parent().prev().children().next().html()

        // 获取单价

        var price = $(that).parent().prev().prev().children().html()
        // 设置价格
        $(that).html(parseInt(kg) * parseInt(price.split("$")[1]))
    }


}

subtoal()
get_prices()

$(function () {
    $(".cart-del").click(function () {

        // 在页面上删除
        console.log($(this).parent().parent().remove())
        // 刷新价格
        get_prices()


        var com_id = $(this).attr("rm_id")

        frame = {com_id: com_id}

        $.ajax({
            url: '/remove_com',
            type: "GET",
            data: frame,
            success: function (resp) {
                alert(resp.errmsg)
            }
        })


    })
})


// 点击添加和减少数量
// 点击减少
$(".sub").click(function () {
    if (parseInt($(this).next().html()) > 0) {
        $(this).next().html(parseInt($(this).next().html()) - 1)
    } else {
        $(this).next().html("0")
    }

    subtoal()
    get_prices()

    var frame = {
        action: "sub",
        com_id: $(this).attr("com_id")
    }

    $.ajax({
        url: "/add_sub",
        type: "POST",
        data: JSON.stringify(frame),
        contentType: "application/json",
    })


})

// 点击添加
$(".add").click(function () {

    $(this).prev().html(parseInt($(this).prev().html()) + 1)
    subtoal()
    get_prices()

    var frame = {
        action: "add",
        com_id: $(this).attr("com_id")
    }

    $.ajax({
        url: "/add_sub",
        type: "POST",
        data: JSON.stringify(frame),
        contentType: "application/json",
    })


})

// 点击提交订单

$("#commit_order").click(function () {

    // 获取参数


})

